<template>
  <el-form label-width="100px" :model="form" :rules="rules" ref="ruleForm">
    <el-form-item label="商品名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>

    <el-form-item label="商品分类" prop="category">
      <el-select v-model="form.category">
        <el-option
          v-for="(item, index) in categoryList"
          :key="index"
          :label="item.cateName"
          :value="item.cateName"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="商品价格" prop="price">
      <el-input-number v-model="form.price"></el-input-number>
    </el-form-item>

    <el-form-item label="商品图片" prop="imgUrl">
      <el-upload
        :action=" BASE_URL + '/goods/goods_img_upload'"
        :on-success="success"
        :show-file-list="false"
        name='img'
        :data={fileName:test}
      >
        <img
          style="width: 100px; height: 100px"
          v-if="form.imgUrl"
          :src="`http://127.0.0.1:3001/upload/imgs/goods_img/${form.imgUrl}`"
          alt=""
        />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>

    <el-form-item label="商品描述" prop="goodsDesc">
      <el-input v-model="form.goodsDesc"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button @click="submit"
        >{{ type === "add" ? "添加" : "编辑" }}商品</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { goodsCateListReq } from "@/api/goods.api.js";
import { BASE_URL } from '@/utils/common'
export default {
  props: {
    type: {
      type: String,
      default: "add",
    },
    defaultForm: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      form: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",  
      },
      categoryList: [],
      BASE_URL,

      rules: {
        name: { required: true, message: "请输入商品名称", trigger: "blur" },
        category: {
          required: true,
          message: "请输入商品分类",
          trigger: "change",
        },
        price: { required: true, message: "请输入商品价格", trigger: "change" },
        imgUrl: {
          required: true,
          message: "请输入商品图片",
          trigger: "change",
        },
        goodsDesc: {
          required: true,
          message: "请输入商品描述",
          trigger: "blur",
        },
      },
    };
  },

  methods: {
    success(res) {
      //   console.log(res);
      this.form.imgUrl = res.imgUrl.split("/").pop();
    },

    submit() {
      //   console.log(this.$refs);
      this.$refs.ruleForm.validate((res) => {
        // console.log("验证规则是否正确", res);
        if (res) {
          this.$emit("form", this.form);
        }
      });
    },
  },

  created() {
    goodsCateListReq().then((res) => {
      console.log(res);
      res.data.data.forEach(item => {
        if(item.state){
          this.categoryList.push(item)
        }
      });
      
    });
  },

  watch: {
    defaultForm: {
      handler: function () {
        if (this.type === "edit") {
            console.log('123');
          this.form = { ...this.defaultForm };
          // 对于图片我们需要特殊处理一下，只取文件名
          if(this.defaultForm.imgUrl){
            this.form.imgUrl = this.defaultForm.imgUrl.split("/").pop();
          }
          
        }
      },
      immediate: true
    },
  },
};
</script>

<style lang="less" scoped>
</style>